import requests

"""
localStorage: 永久有效
sessionStorage：窗口关闭，会话结束自动删除
cookie: 可以指定有效期，多用于存储用户信息 等

"""

# 模拟登录获取cookie
# url = "https://vip.hdbz.net/auth/ajaxlogin"
# data = {
# 	"username": "15138001200",
# 	"userpwd": "pckzzy101"
# }
# res = requests.post(url, data=data)
# if res.status_code == 200:
# 	# RequestsCookieJar
# 	print(res.cookies)
#
# 	# 方式一 直接使用cookiejar
# 	# home_res = requests.get("https://vip.hdbz.net/site/FavoriteList?page=1&limit=10", cookies=res.cookies)
# 	# print(home_res.json())
#
# 	# 方式二 构造cookie字典
# 	# cookies = {
# 	# 	"uid": res.cookies.get("uid"),
# 	# 	"uname": res.cookies.get("uname"),
# 	# 	"unotify": res.cookies.get("unotify"),
# 	# }
# 	# home_res = requests.get("https://vip.hdbz.net/site/FavoriteList?page=1&limit=10", cookies=cookies)
# 	# print(home_res.json())
#
# 	# 方式三 在headers中使用cookie
# 	headers = {
# 		"cookie": f'uid={res.cookies.get("uid")};uname={res.cookies.get("uname")};unotify={res.cookies.get("unotify")}'
# 	}
# 	home_res = requests.get("https://vip.hdbz.net/site/FavoriteList?page=1&limit=10", headers=headers)
# 	print(home_res.json())


# 第一种cooke写法
cooke = {'cookie':
             'Hm_lvt_c555701034a208b65b9f7b81d7aa331f=1743492050; HMACCOUNT=63E8A4DA6FC9F545; '
             'openid=44C5880C5DDAFC27C8DA7C2AE5A733BE; isVip=0; userNickname=NullPointerException; '
             'userimg=https%3A%2F%2Fvip.hdbz.net%2Favatar%2F2025%2F04%2F01%2Flogo_18620.jpg; '
             'Hm_lpvt_c555701034a208b65b9f7b81d7aa331f=1743492416; uname=QQ_44C5880C5DDAFC27C8DA7C2AE5A733BE; '
             'unotify=d3b501c787021d02c55b41740105f2a8; uid=18620'}

req = requests.get('https://vip.hdbz.net/site/FavoriteList?page=1&limit=10', cookies=cooke)
for i in req.json()['data']:
    print(i['favoriteTitle'])

# 第二种cooke写法
req = requests.get('https://vip.hdbz.net/site/FavoriteList?page=1&limit=10', cookies={'cookie':
                                                                                          'Hm_lvt_c555701034a208b65b9f7b81d7aa331f=1743492050; HMACCOUNT=63E8A4DA6FC9F545;'
                                                                                          'openid'
                                                                                          '=44C5880C5DDAFC27C8DA7C2AE5A733BE; isVip=0; userNickname=NullPointerException;'
                                                                                      })
for i in req.json()['data']:
    print(i['favoriteTitle'])

# 第三种cooke写法
headers = {
    'User-Agent': '',
    'Cookie': 'Hm_lvt_c555701034a208b65b9f7b81d7aa331f=1743492050; HMACCOUNT=63E8A4DA6FC9F545; '
              'openid=44C5880C5DDAFC27C8DA7C2AE5A733BE; isVip=0; userNickname=NullPointerException; '}

req = requests.get('https://vip.hdbz.net/site/FavoriteList?page=1&limit=10', headers=headers)
for i in req.json()['data']:
    print(i['favoriteTitle'])





# 如果不能轻松的模拟登录获取cookie， 则可以在浏览器登录之后 手动复制cookie
# 但是随着cookie过期则需要再次手动赋值
headers = {
    "cookie": f'isVip=0; userNickname=15138001200; userimg=https%3A%2F%2Fvip.hdbz.net%2Favatar%2Fnoavatar.png; Hm_lvt_c555701034a208b65b9f7b81d7aa331f=1743479255,1743490483; Hm_lpvt_c555701034a208b65b9f7b81d7aa331f=1743491018; HMACCOUNT=015F91C31DF7EE50; uname=15138001200; unotify=336e9db90a4b65aef3c34518312c5e1a; uid=13303'
}
# favorite_res = requests.get("https://vip.hdbz.net/site/FavoriteList?page=1&limit=10", headers=headers)
# print(favorite_res.json())


download_res = requests.get("https://vip.hdbz.net/site/downloadlist?page=1&limit=10", headers=headers)
print(download_res.json())
